<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <title>Gogs中文文档 - 书栈(BookStack.CN)</title>
    <link href="editormd.css" rel="stylesheet">
</head>
<body>
    <h1 id="article-title">1.2 常见问题</h1>
    <div class="article-body markdown-body editormd-preview-container"  id="page-content">
        <div class="markdown-toc editormd-markdown-toc"><ul class="markdown-toc-list"><li><a class="toc-level-1" href="#常见问题" level="1">常见问题</a><ul><li><a class="toc-level-3" href="#部署应用" level="3">部署应用</a><ul><li><a class="toc-level-4" href="#如果我已经有其它应用使用端口 3000 了怎么办？" level="4">如果我已经有其它应用使用端口 3000 了怎么办？</a></li><li><a class="toc-level-4" href="#如何使用 NGINX 的反向代理？" level="4">如何使用 NGINX 的反向代理？</a><ul><li><a class="toc-level-5" href="#配置子路径" level="5">配置子路径</a></li><li><a class="toc-level-5" href="#为什么上传大文件时总是发生错误？" level="5">为什么上传大文件时总是发生错误？</a></li><li><a class="toc-level-5" href="#如果我用的是 Apache 2 怎么配置子路径？" level="5">如果我用的是 Apache 2 怎么配置子路径？</a></li><li><a class="toc-level-5" href="#有没有用 lighttpd 配置子路径的例子？" level="5">有没有用 lighttpd 配置子路径的例子？</a></li></ul></li><li><a class="toc-level-4" href="#如何使用 HTTPS？" level="4">如何使用 HTTPS？</a></li><li><a class="toc-level-4" href="#如何使用离线模式？" level="4">如何使用离线模式？</a></li><li><a class="toc-level-4" href="#如何使用自定义 robots.txt？" level="4">如何使用自定义 robots.txt？</a></li><li><a class="toc-level-4" href="#如何以守护进程形式运行？" level="4">如何以守护进程形式运行？</a></li><li><a class="toc-level-4" href="#Systemd 服务" level="4">Systemd 服务</a></li></ul></li><li><a class="toc-level-3" href="#管理权限" level="3">管理权限</a><ul><li><a class="toc-level-4" href="#如何成为管理员？" level="4">如何成为管理员？</a></li></ul></li><li><a class="toc-level-3" href="#仓库管理" level="3">仓库管理</a><ul><li><a class="toc-level-4" href="#如何给予用户 Git 钩子权限？" level="4">如何给予用户 Git 钩子权限？</a></li></ul></li><li><a class="toc-level-3" href="#其它" level="3">其它</a><ul><li><a class="toc-level-4" href="#如何获取 Gogs 当前版本？" level="4">如何获取 Gogs 当前版本？</a><ul></ul></li></ul></li></ul></li></ul></div><h1 id="h1-u5E38u89C1u95EEu9898"><a name="常见问题" class="reference-link"></a><span class="header-link octicon octicon-link"></span>常见问题</h1><h3 id="h3-u90E8u7F72u5E94u7528"><a name="部署应用" class="reference-link"></a><span class="header-link octicon octicon-link"></span>部署应用</h3><h4 id="h4--3000-"><a name="如果我已经有其它应用使用端口 3000 了怎么办？" class="reference-link"></a><span class="header-link octicon octicon-link"></span>如果我已经有其它应用使用端口 3000 了怎么办？</h4><p>您可以在您第一次启动 Gogs 的时候通过以下方式修改默认的监听端口：</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pun">./</span><span class="pln">gogs web </span><span class="pun">-</span><span class="pln">port </span><span class="lit">3001</span></code></li></ol></pre><p>该方法同样会在您填写安装页面时生效，因此请选择一个您希望以后一直给 Gogs 使用的端口号。</p>
<h4 id="h4--nginx-"><a name="如何使用 NGINX 的反向代理？" class="reference-link"></a><span class="header-link octicon octicon-link"></span>如何使用 NGINX 的反向代理？</h4><p>在 <code>nginx.conf</code> 文件中，将下面的 <code>server</code> 部分增加至 <code>http</code> 分区内并重载配置：</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pln">server </span><span class="pun">{</span></code></li><li class="L1"><code><span class="pln">    listen </span><span class="lit">80</span><span class="pun">;</span></code></li><li class="L2"><code><span class="pln">    server_name git</span><span class="pun">.</span><span class="pln">crystalnetwork</span><span class="pun">.</span><span class="pln">us</span><span class="pun">;</span></code></li><li class="L3"><code></code></li><li class="L4"><code><span class="pln">    location </span><span class="pun">/</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L5"><code><span class="pln">        proxy_pass http</span><span class="pun">:</span><span class="com">//localhost:3000;</span></code></li><li class="L6"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L7"><code><span class="pun">}</span></code></li></ol></pre><h5 id="h5-u914Du7F6Eu5B50u8DEFu5F84"><a name="配置子路径" class="reference-link"></a><span class="header-link octicon octicon-link"></span>配置子路径</h5><p>如果您想要通过域名的子路径来访问 Gogs 实例，可以将 NGINX 的配置修改为以下形式（特别注意后缀 <code>/</code>）:</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pln">server </span><span class="pun">{</span></code></li><li class="L1"><code><span class="pln">    listen </span><span class="lit">80</span><span class="pun">;</span></code></li><li class="L2"><code><span class="pln">    server_name git</span><span class="pun">.</span><span class="pln">crystalnetwork</span><span class="pun">.</span><span class="pln">us</span><span class="pun">;</span></code></li><li class="L3"><code></code></li><li class="L4"><code><span class="pln">    location </span><span class="pun">/</span><span class="pln">gogs</span><span class="pun">/</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L5"><code><span class="pln">        proxy_pass http</span><span class="pun">:</span><span class="com">//localhost:3000/;</span></code></li><li class="L6"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L7"><code><span class="pun">}</span></code></li></ol></pre><p>然后在配置文件中设置 <code>[server] ROOT_URL = http://git.crystalnetwork.us/gogs/</code>。</p>
<h5 id="h5--"><a name="为什么上传大文件时总是发生错误？" class="reference-link"></a><span class="header-link octicon octicon-link"></span>为什么上传大文件时总是发生错误？</h5><p>想要了解如何让 NGINX 支持大文件上传的讨论，可以查看 <a href="http://stackoverflow.com/a/15021750">此贴</a> 。一般 NGINX 会返回 <code>413</code> 错误，在配置文件中加入以下内容可以解决该问题：</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pln">client_max_body_size </span><span class="lit">50m</span><span class="pun">;</span></code></li></ol></pre><h5 id="h5--apache-2-"><a name="如果我用的是 Apache 2 怎么配置子路径？" class="reference-link"></a><span class="header-link octicon octicon-link"></span>如果我用的是 Apache 2 怎么配置子路径？</h5><p>可以尝试使用下面的配置模板：</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="tag">&lt;VirtualHost</span><span class="pln"> *:443</span><span class="tag">&gt;</span></code></li><li class="L1"><code><span class="pln">        ...</span></code></li><li class="L2"><code><span class="pln">        </span><span class="tag">&lt;Proxy</span><span class="pln"> *</span><span class="tag">&gt;</span></code></li><li class="L3"><code><span class="pln">                 Order allow,deny</span></code></li><li class="L4"><code><span class="pln">                 Allow from all</span></code></li><li class="L5"><code><span class="pln">        </span><span class="tag">&lt;/Proxy&gt;</span></code></li><li class="L6"><code></code></li><li class="L7"><code><span class="pln">        ProxyPass /git http://127.0.0.1:3000/</span></code></li><li class="L8"><code><span class="pln">        ProxyPassReverse /git http://127.0.0.1:3000/</span></code></li><li class="L9"><code><span class="tag">&lt;/VirtualHost&gt;</span></code></li></ol></pre><h5 id="h5--lighttpd-"><a name="有没有用 lighttpd 配置子路径的例子？" class="reference-link"></a><span class="header-link octicon octicon-link"></span>有没有用 lighttpd 配置子路径的例子？</h5><p>可以尝试使用下面的配置模板：</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pln">server</span><span class="pun">.</span><span class="pln">modules  </span><span class="pun">+=</span><span class="pln"> </span><span class="pun">(</span><span class="pln"> </span><span class="str">&#34;mod_proxy_backend_http&#34;</span><span class="pln"> </span><span class="pun">)</span></code></li><li class="L1"><code><span class="pln">$HTTP</span><span class="pun">[</span><span class="str">&#34;url&#34;</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=~</span><span class="pln"> </span><span class="str">&#34;^/gogs&#34;</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L2"><code><span class="pln">        proxy</span><span class="pun">-</span><span class="pln">core</span><span class="pun">.</span><span class="pln">protocol </span><span class="pun">=</span><span class="pln"> </span><span class="str">&#34;http&#34;</span></code></li><li class="L3"><code><span class="pln">        proxy</span><span class="pun">-</span><span class="pln">core</span><span class="pun">.</span><span class="pln">backends </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="pln"> </span><span class="str">&#34;localhost:3000&#34;</span><span class="pln"> </span><span class="pun">)</span></code></li><li class="L4"><code><span class="pln">        proxy</span><span class="pun">-</span><span class="pln">core</span><span class="pun">.</span><span class="pln">rewrite</span><span class="pun">-</span><span class="pln">request </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span></code></li><li class="L5"><code><span class="pln">          </span><span class="str">&#34;_uri&#34;</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="pun">(</span><span class="pln"> </span><span class="str">&#34;^/gogs/?(.*)&#34;</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="str">&#34;/$1&#34;</span><span class="pln"> </span><span class="pun">),</span></code></li><li class="L6"><code><span class="pln">          </span><span class="str">&#34;Host&#34;</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="pun">(</span><span class="pln"> </span><span class="str">&#34;.*&#34;</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="str">&#34;localhost:3000&#34;</span><span class="pln"> </span><span class="pun">),</span></code></li><li class="L7"><code><span class="pln">        </span><span class="pun">)</span></code></li><li class="L8"><code><span class="pun">}</span></code></li></ol></pre><h4 id="h4--https-"><a name="如何使用 HTTPS？" class="reference-link"></a><span class="header-link octicon octicon-link"></span>如何使用 HTTPS？</h4><p>在 <code>custom/conf/app.ini</code> 文件中修改下列配置选项（以下仅为示例）：</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pun">[</span><span class="pln">server</span><span class="pun">]</span></code></li><li class="L1"><code><span class="pln">PROTOCOL </span><span class="pun">=</span><span class="pln"> https</span></code></li><li class="L2"><code><span class="pln">ROOT_URL </span><span class="pun">=</span><span class="pln"> https</span><span class="pun">:</span><span class="com">//try.gogs.io/</span></code></li><li class="L3"><code><span class="pln">CERT_FILE </span><span class="pun">=</span><span class="pln"> custom</span><span class="pun">/</span><span class="pln">https</span><span class="pun">/</span><span class="pln">cert</span><span class="pun">.</span><span class="pln">pem</span></code></li><li class="L4"><code><span class="pln">KEY_FILE </span><span class="pun">=</span><span class="pln"> custom</span><span class="pun">/</span><span class="pln">https</span><span class="pun">/</span><span class="pln">key</span><span class="pun">.</span><span class="pln">pem</span></code></li></ol></pre><p>如果您想要使用自签名的 HTTPS，则可以使用下列命令来生成所需文件（需要使用构建标签 <code>cert</code> 或直接从官方下载二进制）：</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pln">$ </span><span class="pun">./</span><span class="pln">gogs cert </span><span class="pun">-</span><span class="pln">ca</span><span class="pun">=</span><span class="kwd">true</span><span class="pln"> </span><span class="pun">-</span><span class="pln">duration</span><span class="pun">=</span><span class="lit">8760h0m0s</span><span class="pln"> </span><span class="pun">-</span><span class="pln">host</span><span class="pun">=</span><span class="pln">myhost</span><span class="pun">.</span><span class="pln">example</span><span class="pun">.</span><span class="pln">com</span></code></li></ol></pre><h4 id="h4--"><a name="如何使用离线模式？" class="reference-link"></a><span class="header-link octicon octicon-link"></span>如何使用离线模式？</h4><p>如果您需要将 Gogs 运行于内网环境下，只需将 <code>custom/conf/app.ini</code> 文件中的配置选项 <code>server -&gt; OFFLINE_MODE</code> 修改为 <code>true</code> 即可。</p>
<h4 id="h4--robots-txt-"><a name="如何使用自定义 robots.txt？" class="reference-link"></a><span class="header-link octicon octicon-link"></span>如何使用自定义 robots.txt？</h4><p>在 <code>custom</code> 目录下创建 <code>robots.txt</code> 文件即可。</p>
<h4 id="h4--"><a name="如何以守护进程形式运行？" class="reference-link"></a><span class="header-link octicon octicon-link"></span>如何以守护进程形式运行？</h4><p>Gogs 拥有一些由第三方提供的脚本来支持以守护进程形式运行：</p>
<ul>
<li><a href="https://github.com/gogits/gogs/blob/master/scripts/init/centos/gogs">init.d/centos</a></li><li><a href="https://github.com/gogits/gogs/blob/master/scripts/init/debian/gogs">init.d/debian</a></li><li>下小节中的 Systemd 服务</li></ul>
<h4 id="h4-systemd-"><a name="Systemd 服务" class="reference-link"></a><span class="header-link octicon octicon-link"></span>Systemd 服务</h4><p>在 GitHub 上的 Gogs 仓库有一个 <a href="https://github.com/gogits/gogs/blob/master/scripts/systemd/gogs.service">systemd 服务模版文件</a>，您需要做出一定的修改才能够使用它：</p>
<ol>
<li>更新 <code>User</code>、<code>Group</code>、<code>WorkingDirectory</code>、<code>ExecStart</code> 和 <code>Environment</code> 为相对应的值。其中 <code>WorkingDirectory</code> 为您的 Gogs 实际安装路径根目录。</li><li>[可选] 如果您 Gogs 安装示例使用 <code>MySQL/MariaDB</code>、<code>PostgreSQL</code>、<code>Redis</code> 或 <code>memcached</code>，请去掉相应 <code>After</code> 属性的注释。</li></ol>
<p>当您完成修改后，请将文件保存至 <code>/etc/systemd/system/gogs.service</code>，然后通过 <code>sudo systemctl enable gogs</code> 命令激活，最后执行 <code>sudo systemctl start gogs</code>。</p>
<p>您可以通过 <code>sudo systemctl status gogs -l</code> 或 <code>sudo journalctl -b -u gogs</code>  命令检查 Gogs 的运行状态。</p>
<h3 id="h3-u7BA1u7406u6743u9650"><a name="管理权限" class="reference-link"></a><span class="header-link octicon octicon-link"></span>管理权限</h3><h4 id="h4--"><a name="如何成为管理员？" class="reference-link"></a><span class="header-link octicon octicon-link"></span>如何成为管理员？</h4><ol>
<li>当用户注册时 <code>ID = 1</code> 则会自动成为管理员，无需邮箱验证。</li><li>默认管理员登录 <code>Admin -&gt; Users</code> 面板并设置其它人员为管理员。</li><li>通过安装页面注册的用户会自动成为管理员。</li></ol>
<h3 id="h3-u4ED3u5E93u7BA1u7406"><a name="仓库管理" class="reference-link"></a><span class="header-link octicon octicon-link"></span>仓库管理</h3><h4 id="h4--git-"><a name="如何给予用户 Git 钩子权限？" class="reference-link"></a><span class="header-link octicon octicon-link"></span>如何给予用户 Git 钩子权限？</h4><p>由于这是一个 <strong>可能损坏您系统的高级权限</strong>，您必须在用户管理面板（<code>/admin/users/:userid</code>）为您完全信任的用户单独开启。</p>
<h3 id="h3-u5176u5B83"><a name="其它" class="reference-link"></a><span class="header-link octicon octicon-link"></span>其它</h3><h4 id="h4--gogs-"><a name="如何获取 Gogs 当前版本？" class="reference-link"></a><span class="header-link octicon octicon-link"></span>如何获取 Gogs 当前版本？</h4><p>纯文本形式的 Gogs 版本存储在文件 <code>templates/.VERSION</code> 中。</p>

    </div>
</body>
</html>